package com.lisa.util.http;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

import javax.servlet.http.HttpServletRequest;

public class Util {
	/**
	 * 通过InputStream流的方式来获取数据
	 * 
	 * @param request
	 * @return
	 * @throws IOException
	 * @throws UnsupportedEncodingException
	 */
	public static String receivePost(HttpServletRequest request) {
		// 读取请求内容
		BufferedReader br = null;
		String reqBody = "";
		try {
			br = new BufferedReader(new InputStreamReader(request.getInputStream(), "UTF-8"));
			String line = null;
			StringBuilder sb = new StringBuilder();
			while ((line = br.readLine()) != null) {
				sb.append(line);
			}
			// 先进行特殊字符转义
			String url = sb.toString().replaceAll("%(?![0-9a-fA-F]{2})", "%25");
			// 将资料解码
			reqBody = URLDecoder.decode(url, "UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {// 使用finally块来关闭输出流、输入流
			try {
				/*if (Utils.objectIsNotEmpty(br)) {*/
					br.close();
				/*}*/
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return reqBody;
	}
}
